<?php
/**
 * Created by PhpStorm.
 * User: gqn
 * Date: 2016/4/11
 * Time: 15:43
 */

namespace admin\Controller;
use Think\Controller;

Class RenovationController extends Controller{

    static public $treelist = array();
    public static function tree($data,$pid=0,$level=0){
        foreach($data as $key=>$value){
            if($value['parent_id']==$pid){
                $value['level'] = $level;
                self::$treelist[] = $value;
                unset($data[$key]);
                self::tree($data,$value['occ_id'],$level+1);
            }
        }
        return self::$treelist;
    }
    public static function tree1($data,$pid=0,$level=0){
        foreach($data as $key=>$value){
            if($value['parent_id']==$pid){
                $value['level'] = $level;
                self::$treelist[] = $value;
                unset($data[$key]);
                self::tree($data,$value['work_id'],$level+1);
            }
        }
        return self::$treelist;
    }

    public function housetype(){
        $data = M('house_class')->where('is_delete = 0')->order('sort asc')->select();
        $this->assign('Renovation',$data);
        $this->display();
    }
    public function delhouse(){
        $id = I('get.id');
        $map['house_id'] = $id;
        $map['is_delete'] = '1';
        $data = M('house_class')->save($map);
        if($data){
            $this->success('删除成功！',U('Renovation/housetype'),1);
            exit;
        }else{
            $this->error('删除失败！');
            exit;
        }
    }

    public function worktime(){
        $data = M('work_time')->where('is_delete = 0')->order('sort asc')->select();
        $this->assign('Work',$data);
        $this->display();
    }

    public function delwork(){
        $id = I('get.id');
        $map['work_time_id'] = $id;
        $map['is_delete'] = '1';
        $data = M('work_time')->save($map);
        if($data){
            $this->success('删除成功！',U('Renovation/worktime'),1);
            exit;
        }else{
            $this->error('删除失败！');
            exit;
        }
    }

    public function addhousetype(){
        if(IS_POST){
            $data['name'] = I('post.house_name');
            $data['desc'] = I('post.desc','');
            $data['order'] = I('post.order');
            $data['status'] = I('post.status');
            $data['add_time'] = time();
            $info = M('house_class')->add($data);
            if($info){
                $this->success('添加成功！',U('Renovation/housetype'),1);
                exit;
            }else{
                $this->error('添加失败！');
                exit;
            }
        }
        $this->display();
    }
    public function edithouse(){
        if(IS_POST){
            $data['house_id'] = I('post.id');
            $data['name'] = I('post.house_name');
            $data['desc'] = I('post.desc','');
            $data['sort'] = I('post.sort');
            $data['status'] = I('post.status');
            $info = M('house_class')->save($data);
            if($info){
                $this->success('修改成功！',U('Renovation/housetype'),1);
                exit;
            }else{
                $this->redirect('Renovation/housetype');
                exit;
            }
        }
        $id = I('get.id');
        $data = M('house_class')->where("house_id = $id")->select();
        $this->assign('data',$data);
        $this->display();
    }

    public function addworktime(){
        if(IS_POST){
            $data['name'] = I('post.work_name');
            $data['order'] = I('post.order');
            $data['status'] = I('post.status');
            $data['add_time'] = time();
            $info = M('work_time')->add($data);
            if($info){
                $this->success('添加成功！',U('Renovation/worktime'),1);
                exit;
            }else{
                $this->error('添加失败！');
                exit;
            }
        }
        $this->display();
    }

    public function editwork(){
        if(IS_POST){
            $data['work_time_id'] = I('post.id');
            $data['name'] = I('post.work_name');
            $data['sort'] = I('post.sort');
            $data['status'] = I('post.status');
            $info = M('work_time')->save($data);
            if($info){
                $this->success('修改成功！',U('Renovation/worktime'),1);
                exit;
            }else{
                $this->redirect('Renovation/worktime');
                exit;
            }
        }
        $id = I('get.id');
        $data = M('work_time')->where("work_time_id = $id")->select();
        $this->assign('data',$data);
        $this->display();
    }

    public function searchhouse(){
        $key = I('get.name');
        $data = M('house_class')->where("name like '%$key%'")->select();
        $this->assign('Renovation',$data);
        $this->display('housetype');
    }

    public function Jobcontent(){
        $data = M('work_unit')->where('is_delete = 0')->order(array('order'=>'asc'))->select();
        $info = self::tree1($data);
        foreach($info as $key=>$value){
            if($info[$key]['parent_id']=='0' && $info[$key]['status']=='2'){
                foreach($info as $k=>$v){
                    if($info[$key]['work_id'] == $info[$k]['parent_id']){
                        unset($info[$k]);
                    }
                }
                unset($info[$key]);
            }
        }
        $this->assign('work',$info);
        $this->display();
    }

    public function addworkcontent(){
        if(IS_POST){
            $data['name'] = I('post.work_name');
            $data['desc'] = I('post.desc','');
            $data['order'] = I('post.sort');
            $data['add_time'] = time();
            $data['status'] = I('post.status');
            $data['occ_id'] = I('post.type');
            $data['standard_RP'] = I('post.standard');
            $data['honour_RP'] = I('post.honour');
            $info = M('work_unit')->add($data);
            if($info){
                $this->success('添加成功！',U('Renovation/Jobcontent'),1);
                exit;
            }else{
                $this->error('添加失败');
                exit;
            }
        }
        $sel = M('occupation')->where('status = 1 and parent_id = 0')->select();
        $this->assign('select',$sel);
        $this->display();
    }

    public function editworkcontent(){
        if(IS_POST){
            $data['work_id'] = I('post.id');
            $data['name'] = I('post.work_name');
            $data['order'] = I('post.sort');
            $data['status'] = I('post.status');
            $data['desc'] = I('post.desc','');
            $data['standard_RP'] = I('post.standard');
            $data['honour_RP'] = I('post.honour');
            $data['parent_id'] = I('post.type');
            $info = M('work_unit')->save($data);
            if($info){
                $this->success('保存成功！',U('Renovation/Jobcontent'),1);
                exit;
            }else{
                $this->redirect('Renovation/Jobcontent');
                exit;
            }
        }
        $id = I('get.id');
        $data = M('work_unit')->where("work_id = $id")->select();
        $sel = M('occupation')->where('status = 1 and parent_id = 0')->select();
        $this->assign('select',$sel);
        $this->assign('work',$data);
        $this->display();
    }

    public function delworkcontent(){
        $id = I('get.id');
        $map['work_id'] = $id;
        $map['is_delete'] = '1';
        $data = M('work_unit')->save($map);
        if($data){
            $this->success('删除成功！',U('Renovation/Jobcontent'),1);
            exit;
        }else{
            $this->error('删除失败！');
            exit;
        }
    }

    public function OccupationManage(){
        $data = M('occupation')->where('is_delete = 0')->order(array('order'=>'asc'))->select();
        $info = self::tree($data);
        foreach($info as $key=>$value){
            if($info[$key]['parent_id']=='0' && $info[$key]['status']=='2'){
                foreach($info as $k=>$v){
                    if($info[$key]['occ_id'] == $info[$k]['parent_id']){
                        unset($info[$k]);
                    }
                }
                unset($info[$key]);
            }
        }
        $this->assign('Occupation',$info);
        $this->display();
    }

    public function addoccupation(){
        if(IS_POST){
            $data['name'] = I('post.occupation_name');
            $data['order'] = I('post.sort');
            $data['status'] = I('post.status');
            $data['desc'] = I('post.desc','');
            $data['add_time'] = time();
            $data['parent_id'] = I('post.type');
            $info = M('occupation')->add($data);
            if($info){
                $this->success('添加成功！',U('Renovation/OccupationManage'),1);
                exit;
            }else{
                $this->error('添加失败！');
                exit;
            }
        }
        $sel = M('occupation')->where('status = 1 and parent_id = 0')->select();
        $this->assign('select',$sel);
        $this->display();
    }

    public function deloccupation(){
        $id = I('get.id');
        $map['occ_id'] = $id;
        $map['is_delete'] = '1';
        $data = M('occupation')->save($map);
        if($data){
            $this->success('删除成功！',U('Renovation/OccupationManage'),1);
            exit;
        }else{
            $this->error('删除失败！');
            exit;
        }
    }

    public function editoccupation(){
        if(IS_POST){
            $data['occ_id'] = I('post.id');
            $data['name'] = I('post.occupation_name');
            $data['order'] = I('post.sort');
            $data['status'] = I('post.status');
            $data['desc'] = I('post.desc','');
            $data['parent_id'] = I('post.type');
            $info = M('occupation')->save($data);
            if($info){
                $this->success('保存成功！',U('Renovation/OccupationManage'),1);
                exit;
            }else{
                $this->redirect('Renovation/OccupationManage');
                exit;
            }
        }
        $id = I('get.id');
        $data = M('occupation')->where("occ_id = $id")->select();
        $sel = M('occupation')->where('status = 1 and parent_id = 0')->select();
        $this->assign('select',$sel);
        $this->assign('Occupation',$data);
        $this->display();
    }

    public function CareerManage(){
        $data = M('occupation')->where('is_delete = 0 and parent_id = 0')->order(array('order'=>'asc'))->select();
        $this->assign('Career',$data);
        $this->display();
    }

    public function addcareer(){
        if(IS_POST) {
            $data['name'] = I('post.career_name');
            $data['order'] = I('post.sort');
            $data['desc'] = I('post.desc','');
            $data['status'] = I('post.status');
            $data['add_time'] = time();
            $info = M('occupation')->add($data);
            $work['name'] = I('post.career_name');
            $work['add_time'] = time();
            $work['status'] = I('post.status');
            M('work_unit')->add($work);
            if($info){
                $this->success('添加成功！',U('Renovation/CareerManage'),1);
                exit;
            }else{
                $this->error('添加失败');
                exit;
            }
        }
        $this->display();
    }

    public function editcareer(){
        if(IS_POST){
            $data['occ_id'] = I('post.id');
            $data['name'] = I('post.career_name');
            $data['order'] = I('post.sort');
            $data['desc'] = I('post.desc','');
            $data['status'] = I('post.status');
            $info = M('occupation')->save($data);
            $map['name'] = I('post.career_name');
            $sub['status'] = I('post.status');
            M('work_unit')->where($map)->save($sub);
            if($info){
                $this->success('修改成功！',U('Renovation/CareerManage'),1);
                exit;
            }else{
                $this->redirect('Renovation/CareerManage');
                exit;
            }
        }
        $id = I('get.id');
        $data = M('occupation')->where("occ_id = $id")->select();
        $this->assign('career',$data);
        $this->display();
    }

    public function delcareer(){
        $id = I('get.id');
        $map['occ_id'] = $id;
        $map['is_delete'] = '1';
        $data = M('occupation')->save($map);
        if($data){
            $this->success('删除成功！',U('Renovation/CareerManage'),1);
            exit;
        }else{
            $this->error('删除失败！');
            exit;
        }
    }

    public function searchcareer(){
        $key = I('get.name');
        $data = M('occupation')->where("name like '%$key%'")->select();
        $this->assign('Career',$data);
        $this->display('CareerManage');
    }

    public function searchoccupation(){
        $key = I('get.name');
        $data = M('occupation')->where("name like '%$key%'")->select();
        $this->assign('Occupation',$data);
        $this->display('OccupationManage');
    }

    public function searchworkcontent(){
        $key = I('get.name');
        $data = M('work_unit')->where("name like '%$key%'")->select();
        $this->assign('work',$data);
        $this->display('Jobcontent');
    }

}